package com.roocon.thread.chapter02;

public class TaxCalculatorMain {

  @FunctionalInterface
  public interface  CalculatorStrategy {
    double calculate(double salary, double bonus);
  }

  public static void main(String[] args) {
    //TaxCalaculator calaculator = new TaxCalaculator(1000d, 20000d){
    //  @Override public double calcTax() {
    //    return getSalary() * 0.1 + getBonus() * 0.15;
    //  }
    //};
    //
    //double tax = calaculator.calculate();

    //2.
    TaxCalaculator calaculator = new TaxCalaculator(10000d, 2000d);
    calaculator.setCalculatorStrategy(((salary, bonus) -> salary * 0.1 + bonus * 0.15));

    //3.
    TaxCalaculator calaculator2 =
        new TaxCalaculator(10000d, 2000d, ((salary, bonus) -> salary * 0.1 + bonus * 0.15));
    System.out.println("result ==> " + calaculator.calcTax());
    System.out.println("result ==> " + calaculator.calcTax());
  }


  static class SimpleCalculatorStrategy implements CalculatorStrategy{

    private final static double SALARY_RATE = 0.1;
    private final static double BONUS_RATE = 0.15;

    @Override public double calculate(double salary, double bonus) {
      return salary * SALARY_RATE + bonus * BONUS_RATE;
    }
  }

  static class TaxCalaculator {

    private final double salary ;
    private final double bonus ;

    public void setCalculatorStrategy(
        CalculatorStrategy calculatorStrategy) {
      this.calculatorStrategy = calculatorStrategy;
    }

    private CalculatorStrategy calculatorStrategy;

    public TaxCalaculator(double salary, double bonus) {
      this.salary = salary;
      this.bonus = bonus;
    }

    public TaxCalaculator(double salary, double bonus, CalculatorStrategy calculatorStrategy) {
      this.salary = salary;
      this.bonus = bonus;
      this.calculatorStrategy = calculatorStrategy;
    }

    public double calcTax(){
      return this.calculatorStrategy.calculate(getSalary(), getBonus());
    }

    public double getSalary() {
      return salary;
    }

    public double getBonus() {
      return bonus;
    }

  }


}
